package com.yanqu.road.server.gameplayer.module.player;

import com.yanqu.road.logic.bussiness.player.UserCollectionBussiness;
import com.yanqu.road.logic.pb.CollectionPb;
import com.yanqu.road.entity.config.building.CollectionInfo;
import com.yanqu.road.entity.config.building.CollectionPavilion;
import com.yanqu.road.entity.config.building.CollectionUpgrade;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserBuilding;
import com.yanqu.road.entity.player.UserCollection;
import com.yanqu.road.entity.player.UserCollectionPavilion;
import com.yanqu.road.pb.building.BuildingProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.config.CollectionMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CollectionModule extends GeneralModule {

    private UserCollectionPavilion userCollectionPavilion;

    private Map<Integer, UserCollection> userCollectionMap;

    public CollectionModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userCollectionPavilion = UserCollectionBussiness.getUserCollectionPavilion(player.getUserId());
        userCollectionMap = UserCollectionBussiness.getUserCollectionMap(player.getUserId());
        initUserCollectionPavilion();
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        if(null != userCollectionPavilion){
            if(userCollectionPavilion.isInsertOption()){
                UserCollectionBussiness.addUserCollectionPavilion(userCollectionPavilion);
            }else if(userCollectionPavilion.isUpdateOption()){
                UserCollectionBussiness.updateUserCollectionPavilion(userCollectionPavilion);
            }
        }
        for(UserCollection userCollection : userCollectionMap.values()){
            if(userCollection.isInsertOption()){
                UserCollectionBussiness.addUserCollection(userCollection);
            }else if(userCollection.isUpdateOption()){
                UserCollectionBussiness.updateUserCollection(userCollection);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        syncUserCollectionPavilion();
        syncUserCollection(new ArrayList<>(userCollectionMap.values()), true);
    }

    private void initUserCollectionPavilion(){
        if(null == userCollectionPavilion){
            UserCollectionPavilion collectionPavilion = new UserCollectionPavilion();
            collectionPavilion.setUserId(player.getUserId());
            collectionPavilion.setLevel(0);
            collectionPavilion.setInsertOption();
            userCollectionPavilion = collectionPavilion;
        }
    }

    public void checkCollectionUnlock(){
        Map<Integer, CollectionInfo> collectionInfoMap = CollectionMgr.getCollectionInfoMap();
        BuildingModule buildingModule = player.getModule(BuildingModule.class);
        List<UserCollection> syncList = new ArrayList<>();
        for(CollectionInfo collectionInfo : collectionInfoMap.values()){
            if(1 == collectionInfo.getUnlockType() && collectionInfo.getNeedBuildingId() > 0 && !userCollectionMap.containsKey(collectionInfo.getId())){
                UserBuilding userBuilding = buildingModule.getBuildingById(collectionInfo.getNeedBuildingId());
                if(null != userBuilding) {
                    if (userBuilding.getLevel() >= collectionInfo.getNeedBuildingLv()) {
                        UserCollection userCollection = initUserCollection(collectionInfo.getId());
                        syncList.add(userCollection);
                    }
                }
            }
        }
        if(syncList.size() > 0){
            syncUserCollection(syncList, false);
        }
    }

    private void syncUserCollectionPavilion(){
        BuildingProto.CollectionPavilionMsg.Builder msg = CollectionPb.parseCollectionPavilionMsg(userCollectionPavilion);
        if(null != msg){
            player.sendPacket(Protocol.U_COLLECTION_PAVILION_SYNC, msg);
        }
    }

    private void syncUserCollection(UserCollection userCollection){
        if(null != userCollection){
            List<UserCollection> syncList = new ArrayList<>();
            syncList.add(userCollection);
            syncUserCollection(syncList, false);
        }
    }

    private void syncUserCollection(List<UserCollection> userCollectionList, boolean isLogin){
        if(isLogin) {
            player.sendPacket(Protocol.U_COLLECTION_LIST, CollectionPb.parseCollectionListMsg(userCollectionList));
        }else {
            player.sendPacket(Protocol.U_COLLECTION_SYNC, CollectionPb.parseCollectionListMsg(userCollectionList));
        }
    }

    public int collectionPavilionUpgrade(){
        CollectionPavilion nextCollectionPavilion = CollectionMgr.getCollectionPavilion(userCollectionPavilion.getLevel() + 1);
        if(null == nextCollectionPavilion){
            return GameErrorCode.E_COLLECTION_PAVILION_MAX_LEVEL;
        }
        int totalCollectionLv = getTotalCollectionLv();
        CollectionPavilion collectionPavilion = CollectionMgr.getCollectionPavilion(userCollectionPavilion.getLevel());
        if(totalCollectionLv < collectionPavilion.getNeedCollectionTotalLv()){
            return GameErrorCode.E_COLLECTION_TOTAL_LEVEL_NO_ENOUGH;
        }
        if(!player.getModule(CurrencyModule.class).removeCurrency(collectionPavilion.getConsume(), eLogMoneyType.Building, eLogMoneyType.CollectionPavilionUpgrade)){
            return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }
        userCollectionPavilion.setLevel(userCollectionPavilion.getLevel() + 1);
        syncUserCollectionPavilion();
        return 0;
    }

    public boolean collectionUpgrade(int collectionId){
        UserCollection userCollection = getUserCollection(collectionId);
        CollectionUpgrade collectionUpgrade = CollectionMgr.getCollectionUpgrade(userCollection.getLevel());
        if(CollectionMgr.isCollectionUpgrade(collectionUpgrade)){//升级成功
            userCollection.setLevel(userCollection.getLevel() + 1);
            syncUserCollection(userCollection);
            return true;
        }
        return false;
    }

    private int getTotalCollectionLv(){
        int totalLv = 0;
        for(UserCollection userCollection : userCollectionMap.values()){
            totalLv += userCollection.getCollectionId();
        }
        return totalLv;
    }

    public int getTotalCollectionAddition(){
        int addition = 0;
        for(UserCollection userCollection : userCollectionMap.values()){
            CollectionUpgrade collectionUpgrade = CollectionMgr.getCollectionUpgrade(userCollection.getLevel());
            addition += collectionUpgrade.getEarnSpeedAddition();
        }
        return addition;
    }

    public UserCollectionPavilion getUserCollectionPavilion() {
        return userCollectionPavilion;
    }

    public UserCollection getUserCollection(int collectionId){
        return userCollectionMap.get(collectionId);
    }

    private UserCollection initUserCollection(int collectionId){
        UserCollection userCollection = new UserCollection();
        userCollection.setUserId(player.getUserId());
        userCollection.setCollectionId(collectionId);
        userCollection.setLevel(1);
        userCollection.setInsertOption();
        userCollectionMap.put(userCollection.getCollectionId(), userCollection);
        return userCollection;
    }
}
